package offer.algorithm;

import java.util.ArrayList;

public class NumSum41 {
    public static ArrayList<ArrayList<Integer>> findContinuousSequence(int sum) {
        ArrayList<ArrayList<Integer>> result = new ArrayList<>();
        if(sum < 3)
            return result;
        int start = 1,cur = 2,curSum = 1,end = (sum >> 1)+2;
        for(;cur <= sum && cur <= end;){
            if(curSum == sum){
                ArrayList<Integer> list = new ArrayList<>();
                for(int i = start;i<cur;++i)
                    list.add(i);
                result.add(list);
                curSum -= start;
                start++;
            }else{
                if(curSum < sum){
                    curSum += cur;
                    cur++;
                }else {
                    while (curSum > sum) {
                        curSum -= start;
                        start++;
                    }
                }
            }
        }
        return result;
    }

    public static void main(String[] args) {
        ArrayList<ArrayList<Integer>> continuousSequence = findContinuousSequence(3);
        for (ArrayList<Integer> integers : continuousSequence){
            System.out.println(integers);
        }
    }
}
